//
//  KFWLabelControlView.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 15/2/2.
//  Copyright (c) 2015年 张明磊. All rights reserved.
//


#import "KFWLabelControlView.h"

@implementation KFWLabelControlView
@synthesize arr_labName;
@synthesize widthOfLabel;
@synthesize selectView;
@synthesize delegate;
@synthesize selectTag;

- (id)initWithLabNameArr:(NSArray *)arr withFrame:(CGRect)aframe
{
    self = [super initWithFrame:aframe];
    if (self) {
        self.arr_labName = arr;
        widthOfLabel = MainScreenWidth/arr.count;
        [self creatLabel];
    }
    return self;
}

- (void)creatLabel
{
    for (int i = 0; i < arr_labName.count; i++) {
        NSString *str = [arr_labName objectAtIndex:i];
        UILabel *label = [UIUtils creatLabel];
        label.frame = CGRectMake(widthOfLabel * i, 0, widthOfLabel, 44);
        label.userInteractionEnabled = YES;
        label.text = str;
        label.font = FontOfHelveticaBold(15);
        label.textColor = LabelUnSelectTextColor;
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = TagOfView + i;

        CALayer *layer_bottom = [[CALayer alloc] init];
        layer_bottom.borderColor = LabelUnSelectTextColor.CGColor;
        layer_bottom.borderWidth = 0.5;
        layer_bottom.frame = CGRectMake(0, label.frame.size.height, label.frame.size.width, 0.5);
        [label.layer addSublayer:layer_bottom];

        if (i != arr_labName.count - 1) {
            CALayer *layer_left = [[CALayer alloc] init];
            layer_left.borderColor = LabelUnSelectTextColor.CGColor;
            layer_left.borderWidth = 0.5;
            layer_left.frame = CGRectMake(label.frame.size.width - 1, 10, 0.5, 24);
            [label.layer addSublayer:layer_left];
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelControlTap:)];
        [label addGestureRecognizer:tap];
        [self addSubview:label];
    }
    
    if (arr_labName.count > 0) {
        selectView = [[UIView alloc] initWithFrame:CGRectMake(0, 44 - 4, widthOfLabel, 4)];
        selectView.backgroundColor = RGB(91, 165, 229);
        UILabel *selectLab = (UILabel *)[self viewWithTag:TagOfView + 0];
        selectLab.textColor = LabelSelectTextColor;
        self.selectTag = TagOfView + 0;
        [self addSubview:selectView];
    }
    
    UIView *view_dibu = [[UIView alloc] initWithFrame:CGRectMake(0, 43, MainScreenWidth, 1)];
    view_dibu.backgroundColor = RGB(223, 223, 223);
    [self addSubview:view_dibu];
}

- (void)labelControlTap:(UIGestureRecognizer *)tap
{
    UILabel *lab = (UILabel *)tap.view;
    if (selectTag != lab.tag)
    {
        UILabel *beforeLab = (UILabel *)[self viewWithTag:selectTag];
        beforeLab.textColor = LabelUnSelectTextColor;
        lab.textColor = LabelSelectTextColor;
        NSInteger tag = tap.view.tag - TagOfView;
        selectTag = tap.view.tag;
        [UIView animateWithDuration:0.5 animations:^{
            selectView.frame = CGRectMake(widthOfLabel*tag, 44 - 4, widthOfLabel, 4);
        }];
        if (delegate && [delegate respondsToSelector:@selector(clickLabelWithTag:)])
        {
            [self.delegate clickLabelWithTag:tag];
        }
    }
}

@end
